<?php
class PhotosController extends AppController {

	var $name = 'Photos';
	var $helpers = array('Html', 'Form');
	var $components = array("Image");
	//var $components = array("Image","RequestHandler");

	function beforeFilter() {
		parent::beforeFilter();

		if($this->Admin!=1)
		{
			$this->Auth->deny('*');
				
		}
		else
		{
			$this->Auth->allow('*');
		}
	}

	function index() {
		$this->Photo->recursive = 0;
		$this->set('photos', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Photo.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('photo', $this->Photo->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
				
			$this->Photo->create();
			if ($this->Photo->save($this->data)) {
				//print_r($this->data['Photo']['Image/name1']['name']  );
				$image_path = $this->Image->upload_image_and_thumbnail($this->data['Photo']['Image/name1'], 573,380,80,80, "photos");
				if(isset($image_path)) {
					$this->Photo->saveField('urlPhoto',$image_path);
				}
				else {
					$this->Session->setFlash(__('The image for the set could not be saved. Please, try again.'.$this->data,"name1", true));
				}
			}

		}
		$items = $this->Photo->Item->find('list');
		$this->set(compact('items'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Photo', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Photo->save($this->data)) {
				$this->Session->setFlash(__('The Photo has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Photo could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Photo->read(null, $id);
		}
		$items = $this->Photo->Item->find('list');
		$this->set(compact('items'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Photo', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Photo->del($id)) {
			$this->Session->setFlash(__('Photo deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}



}
?>